/**
 * @author Linrui
 * @since 2011-05-06
 * 校园信息管理
 */
package com.qeado.colleage.web;

import java.io.File;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.qeado.news.model.News;
import com.qeado.news.service.NewsService;
import com.qeado.sort.model.Sort;
import com.qeado.sort.service.SortService;
import com.qeado.user.model.User;
import com.qeado.util.FileUpload;
import com.qeado.util.PageHelper;

@Scope("request")
@Controller
public class ColleageManageAction extends ActionSupport {

	@Autowired
	private SortService sortService;
	@Autowired
	private NewsService newsService;

	private List<Sort> sortList; // 信息类别
	private News news; // 新闻DTO
	private PageHelper publicInfo; // 已发布的信息
	private String pageNum = "1"; // 当前页码
	private String pageSize = "10";

	private File myFile; // 上传的文件
	private String contentType; // 上传的文件类型
	private String fileName; // 上传文件的文件名

    public void setMyFileContentType(String contentType) {
        this.contentType = contentType;
   } 
   
    public void setMyFileFileName(String fileName) {
        this.fileName = fileName;
   } 
       
    public void setMyFile(File myFile) {
        this.myFile = myFile;
   } 
	
	public String getPageSize() {
		return pageSize;
	}

	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}

	public String getPageNum() {
		return pageNum;
	}

	public void setPageNum(String pageNum) {
		this.pageNum = pageNum;
	}

	public PageHelper getPublicInfo() {
		return publicInfo;
	}

	public void setPublicInfo(PageHelper publicInfo) {
		this.publicInfo = publicInfo;
	}

	public News getNews() {
		return news;
	}

	public void setNews(News news) {
		this.news = news;
	}

	public List<Sort> getSortList() {
		return sortList;
	}

	public void setSortList(List<Sort> sortList) {
		this.sortList = sortList;
	}

	@Override
	public String execute() throws Exception {
		return super.execute();
	}

	/* 预先加载类别信息 */
	public String publicInfoPage() throws Exception {
		if (sortList == null)
			sortList = sortService.listByPcode("SH001");
		return "public_colleage_info";
	}

	public String publicInfo() throws Exception {
		String statu;
		User user = (User) ActionContext.getContext().getSession()
				.get("login_user");
		FileUpload upload = null;
		if (myFile != null) {
			upload = new FileUpload();
			upload.setBasePath(ServletActionContext.getServletContext()
					.getRealPath("uploadfiles"));
			upload.setUserDir(user.getId());
			upload.setMyFile(myFile);
			upload.setFileName(fileName);
		}
		news.setCreator(user.getUserName());
		statu = newsService.insertNews(news);
		if ("success".equals(statu)) {
			pageNum = "1"; // 跳转到第一页
			return listInfo();
		} else {
			return "fail";
		}
	}

	/* 分页查询已发布的纪录 */
	public String listInfo() throws Exception {
		User user = (User) ActionContext.getContext().getSession()
				.get("login_user");
		if (user != null) {
			publicInfo = newsService.listByUserName(Integer.parseInt(pageNum),
					user.getUserName());
			publicInfo.setPagebar("colleage_manage!listInfoByUser");
			return "info_list";
		} else
			return "fail";
	}

}
